<template>
    <!-- 筛选区位置 -->
   <div class="sub-filter" v-if='filterParams && !filterLoading'>

    <!-- 品牌筛选 -->
     <div class="item">
       <div class="head">品牌：</div>
       <div class="body">
         <a href="javascript:;" @click='filterParams.brands.selectedBrand=item.id' :class='{active: filterParams.brands.selectedBrand===item.id}' v-for="item in filterParams.brands.list" :key="item.id">{{item.name}}</a>
       </div>
     </div>

     <!-- 其他筛选 -->
    <div class="item" v-for="item in filterParams.saleProperties" :key="item.id">
      <div class="head">{{item.name}}：</div>
      <div class="body">
        <a  href="javascript:;" @click='item.selectedProp=tag.id' :class='{active: item.selectedProp===tag.id}' v-for="tag in item.properties" :key="tag.id">{{tag.name}}</a>
      </div>
    </div>

   </div>

    <!-- 骨架屏组件 -->
    <div v-else class="sub-filter">
    <XtxSkeleton class="item" width="800px" height="40px" />
    <XtxSkeleton class="item" width="800px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
    </div>

</template>
<script>
import { findSubCategoryFilter } from '@/api/category'
import { watch, ref } from 'vue'
import { useRoute } from 'vue-router'
export default {
  name: 'SubFilter',
  emits: ['filter-change'],
  setup (props, { emit }) {
    const route = useRoute()
    // * 作用:切换二级目录的时候也有骨架屏
    const filterLoading = ref(false)

    // * 筛选栏所有的数据
    const filterParams = ref(null)

    watch(() => route.params.id, (newVal) => {
      if (route.fullPath === '/category/sub/' + newVal) {
        findSubCategoryFilter(newVal).then(ret => {
          // 1- 在品牌的数据前面加上全部
          if (ret.result.brands) {
            // 添加选中的属性，用于记录选中的条件值
            ret.result.brands.selectedBrand = null
            ret.result.brands.unshift({
              id: null,
              name: '全部'
            })
          }
          // 2- 在其他选项数据前面加上全部
          if (ret.result.saleProperties) {
            ret.result.saleProperties.forEach(item => {
              // 添加选中的属性，用于记录选中的条件值
              item.selectedProp = null
              item.properties.unshift({
                id: null,
                name: '全部'
              })
            })
          }

          // 3 - 将筛选项里面所有的数据合并到filterParams中
          filterParams.value = {
            brands: {
              list: ret.result.brands,
              selectedBrand: null
            },
            saleProperties: ret.result.saleProperties
          }
          filterLoading.value = false
        })
      }
    }, {
      immediate: true
    })

    watch(() => filterParams, (newVal) => {
      const attrs = []
      newVal.value.saleProperties && newVal.value.saleProperties.forEach(item => {
        const param = {
          // 左侧名称
          groupName: item.name,
          // 右侧选项名称
          propertyName: item.selectedProp ? item.properties.find(tag => tag.id === item.selectedProp).name : null
        }
        attrs.push(param)
      })
      emit('filter-change', {
        brandId: filterParams.value.brands.selectedBrand,
        arttrs: attrs
      })
    }, { deep: true })
    return { filterParams, filterLoading }
  }
}
</script>
<style scoped lang='less'>
  // 筛选区
  .sub-filter {
    background: #fff;
    padding: 25px;
    .xtx-skeleton {
    padding: 10px 0;
}
    .item {
      display: flex;
      line-height: 40px;
      .head {
        width: 80px;
        color: #999;
      }
      .body {
        flex: 1;
        a {
          margin-right: 36px;
          transition: all .3s;
          display: inline-block;
          &.active,
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
</style>
